package com.kkb.shopcenter.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.shopcenter.api.UserInfoService;
import com.kkb.shopcenter.common.common.HttpResult;
import com.kkb.shopcenter.common.pojo.UserInfo;
import io.swagger.annotations.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@Api(value = "用户管理", description = "用户管理")//@Api：⽤在类上，说明该类的作⽤。
@RestController
@RequestMapping("/api/user")
public class UserInfoController {
    @Reference
    private UserInfoService userInfoService;

    @GetMapping("/alluser")
    public HttpResult getAllUser() {
        return HttpResult.success(userInfoService.getAllUserInfo());
    }

    @PostMapping("/addUser")
    public HttpResult addUser(@RequestBody UserInfo userInfo) {
        return HttpResult.success(userInfoService.addUserInfo(userInfo)) ;
    }

    @PutMapping("/updateUser")
    public HttpResult updateUser(@RequestBody UserInfo userInfo) {
        return HttpResult.success(userInfoService.updateUserInfo(userInfo));
    }

    @DeleteMapping("/delUser/{id}")
    public HttpResult delUserById(@PathVariable Long id) {
        return HttpResult.success(userInfoService.deleteUserInfo(id));
    }

    //分页查询接口说明，Swagger处用到
    @ApiOperation(value = "用户分页查询",notes = "接受分页参数page,pageSize")
    @GetMapping("/userList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "page", value = "第几页", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页显示多少条数据", required = true, dataType = "int")})
    @ApiResponses({
            @ApiResponse(code = 200, message = "条件分页查询用户成功"),
            @ApiResponse(code = 500, message = "条件分页查询用户失败,后台服务出现异常"),
            @ApiResponse(code = 401, message = "代表此操作无权限访问"),
            @ApiResponse(code = 400, message = "表示请求参数错误")})
    public HttpResult getPageUserInfo(@RequestParam(value = "page" ,defaultValue = "1") int pageNum,
                                              @RequestParam(value = "pageSize" ,defaultValue = "1") int pageSize) {
        PageInfo<UserInfo> pageUserInfo = userInfoService.getPageUserInfo(pageNum, pageSize);
        return HttpResult.success(pageUserInfo);
    }

//    //模拟异常处理
//    @RequestMapping("/getException")
//    public HttpResult Exception() {
//        HttpResult result = new HttpResult();
//        int a=1/0;
//        return result;
//    }
//    //模拟异常处理
//    @RequestMapping("/getException2")
//    public HttpResult Exception2() {
//        HttpResult result = null;
//        result.getCode();
//        return result;
//    }
}
